package com.leadbank.baselbf.leadutils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.Display;
import android.view.WindowManager;

import com.leadbank.baselbf.base.BaseLBFApplication;
import com.leadbank.baselbf.log.LogBus;
import com.leadbank.baselbf.manager.PreferencesKey;
import com.leadbank.baselbf.manager.SPLBFManager;
import com.leadbank.baselbf.outils.DeviceInfo;
import com.leadbank.baselbf.user.AppUserInfo;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

/**
 * @ClassName: CommonUtils
 * @Description:
 * @Author: zhouzhiguo
 * @Date: 2024/5/27 15:44
 * @Version: 5.0.6
 * 上海利得金融科技集团版权所有.
 */

public class CommonUtils {

    /**
     * 获取设备信息 ， 接口输入参数
     *
     * @param context ""
     * @return String    返回类型
     */
    @SuppressLint("MissingPermission")
    public static String getDeviceInfo(Context context) {
        boolean isAgreePrivacy = isAgreePrivacy(context);
        if (!isAgreePrivacy) {
            return "000";
        }
        LogBus.d("CommonUtils","getDeviceInfo() 执行");
        String info;
        String deviceId = AppUserInfo.getDeviceID();
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//        if (Build.VERSION.SDK_INT >= 23 && context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
//                != PERMISSION_GRANTED) {
//            //ToastUtil.show("不能获取手机ID，部分功能将不可用！");
////            deviceId = "-";
//            deviceId = SPLBFManager.getString(context, PreferencesKey.UMENG_DEVICE_TOKEN, "-");
//        } else {//已授权
//            if (isAgreePrivacy) {
//                deviceId = DeviceInfo.getDeviceUniqueId(context);//设备ID
//            }
//
//        }

        String simOperatorName = tm.getSimOperator();//运营商名称

        WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display mDisplay = window.getDefaultDisplay();
        int W = mDisplay.getWidth();//分辨率
        int H = mDisplay.getHeight();

        String release = Build.VERSION.RELEASE;//系统版本号
        String model = Build.MODEL;//手机型号

        //获取网络连接管理者 ，联网方式
        ConnectivityManager connectionManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取网络的状态信息，有下面三种方式
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
        String type1 = "";
        if (networkInfo != null) {
            int type = networkInfo.getType();
            if (type == ConnectivityManager.TYPE_MOBILE) {
                type1 = networkInfo.getSubtype() + "";
            } else {
                type1 = "wifi";
            }
        }
        //渠道号
        String channel = AppInfo.getAppChannel(context);
        //设备号|机型|分别率|操作系统|联网方式|运营商
        info = deviceId + "|" + model + "|" + W + "x" + H + "|" + release + "|" + type1 + "|" + simOperatorName + "|" + channel + "|";
        LogBus.d("CommonUtil","info = "+ info);
        return info;
    }
    /**
     * 获取设备唯一标识码
     */
    public static String getGUID(Context context) {
        String id;


//        if ("Y".equals(preferences.getString(CommonKey.BROWSER_MODE,"")) && BaseLBFApplication.browserModel){
//            //浏览模式不获取设备信息
//            return "";
//        }
        if (!isAgreePrivacy(context)){
            return "";
        }

        if (Build.VERSION.SDK_INT >= 23 && context.checkSelfPermission( Manifest.permission.READ_PHONE_STATE)
                != PERMISSION_GRANTED) {
            //ToastUtil.show("不能获取手机ID，部分功能将不可用！");
//            id = "-";
            id = SPLBFManager.getString(context,PreferencesKey.UMENG_DEVICE_TOKEN,"-");
        } else {//已授权
            id = DeviceInfo.getDeviceID(context);
        }

        if (TextUtils.isEmpty(id)) {
            SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(context);
            id = preference.getString("guid", null);
            if (id == null) {
                id = "android_" + java.util.UUID.randomUUID().toString();
                preference.edit().putString("guid", id).commit();
            }
        }
        return id;
    }

    /**
     * 是否同意隐私
     *
     * @return
     */
    public static boolean isAgreePrivacy(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PreferencesKey.PRIVACY, Context.MODE_PRIVATE);
        String oldVersion = preferences.getString(PreferencesKey.PRIVACY_VERSION, "").trim();
        String browserMode = preferences.getString(PreferencesKey.BROWSER_MODE, "Y");
        String pricacyProtocolVersion = preferences.getString(PreferencesKey.pricacyProtocolVersion, "");
//       if (
//               ("Y".equals(browserMode)|| !oldVersion.equals(pricacyProtocolVersion))
//               && BaseLBFApplication.browserModel){
//            //浏览模式不获取设备信息
//            return false;
//        }

        if ("Y".equals(browserMode) && BaseLBFApplication.browserModel) {
            //浏览模式不获取设备信息
            return false;
        }
        return true;
    }
}
